package com.isljj.web_chat2.controller;

import com.isljj.web_chat2.constant.Constant;
import com.isljj.web_chat2.mapper.UserMapper;
import com.isljj.web_chat2.model.Result;
import com.isljj.web_chat2.model.User;
import com.isljj.web_chat2.utils.JWTUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private JWTUtils jwtUtils;
    //    查询用户 返回用户列表 模糊搜索 排除自己和已经有的好友
    @RequestMapping("/findFriend")
    public Result<List> findFriend(String toUserName,HttpServletRequest request){
        String jwtToken = request.getHeader(Constant.REQUEST_TOKEN_KEY);

        User loginUser = jwtUtils.getUserByToken(jwtToken);

        List<User> list = userMapper.selectByName2(toUserName,loginUser.getUserId());
        for(User user:list){
            user.setPassword("");
        }
        return new Result<>(200,"查询成功",list);
    }


//    页面加载时,个人信息 从后端拿
    @RequestMapping("/userInfo")
    public Result<User> getUserInfo(HttpServletRequest request, HttpServletResponse response){

//        获取token
        String jwtToken = request.getHeader(Constant.REQUEST_TOKEN_KEY);
        if(jwtToken==null){
            response.setStatus(401);
            return  null;
        }
//        log.info("jwtToken - "+jwtToken);
        User user = jwtUtils.getUserByToken(jwtToken);

        if(user!=null){
            user.setPassword("");
            return new Result<>(200,"成功拿到用户信息",user);
        }else {
            return new Result<>(Constant.FAIL_CODE,"未能拿到用户信息",new User());
        }
    }

    @RequestMapping ("/login")
    public Result<String> login(String username, String password, HttpSession session){
        //1.查询数据库
        User user = userMapper.selectByName(username);
        System.out.println("登录用户信息 - " + user.toString());
        if(user == null||!user.getPassword().equals(password)){
            //密码错误或者用户名为空
            System.out.println("登录失败!"+user);
            return new Result<>(Constant.FAIL_CODE,"登录失败","");
        }
        //2.成功之后创建会话 --->   token
//        session.setAttribute(Constant.LOGINUSER_SESSION_KEY,user);
        Map<String,Object> claims = new HashMap<>();
        claims.put("userId",user.getUserId());
        claims.put("userName",user.getUserName());

        String token = jwtUtils.genToken(claims);
        user.setPassword("");
        log.info("登陆成功");
        return new Result<>(Constant.SUCCESS_CODE,"登录成功",token);
    }

    @RequestMapping("/register")
    public Result<User> register(String username,String password){

        User user = null;
        try {
            user = new User();
            user.setUserName(username);
            user.setPassword(password);
            userMapper.insertUser(user);
            System.out.println("注册成功 username :"+username);
        } catch (DuplicateKeyException e) {
            //名字重复,注册失败 ,抛出异常
            System.out.println("注册失败 username :" + username);
            user = new User();
            return new Result<>(Constant.FAIL_CODE,"注册失败",user);
        }
        user.setPassword("");
        return new Result<>(Constant.SUCCESS_CODE,"注册成功",user);
    }
}
